﻿#define _CRT_SECURE_NO_WARNINGS 1

#include "Contact.h"

void CheckCapacity(Contact* pc);


void LoadContact(Contact* pc)
{
	//打开文件
	FILE* pf = fopen("Contact.txt", "rb");
	if (NULL == pf)
	{
		perror("fopen");
		return;
	}
	//使用
	PeoInfo tmp = { 0 };
	while (fread(&tmp, sizeof(PeoInfo), 1, pf))
	{
		CheckCapacity(pc);
		pc->data[pc->size] = tmp;
		pc->size++;
	}

	//关闭文件
	fclose(pf);
	pf = NULL;

}
void InitContact(Contact* pc)
{
	pc->size = 0;
	pc->capacity = MAX_CAPACITY;
	pc->data = (PeoInfo*)malloc(sizeof(PeoInfo) * pc->capacity);
	if (NULL == pc->data)
	{
		perror("malloc");
		exit(-1);
	}

	//从文件导入联系人
	LoadContact(pc);
	
}
void CheckCapacity(Contact* pc)
{
	//满了，扩容
	if (pc->size == pc->capacity)
	{
		PeoInfo* tmp = (PeoInfo*)realloc(pc->data, sizeof(PeoInfo) * (pc->capacity + INC_SIZE));
		if (tmp == NULL)
		{
			printf("realloc：%s", strerror(errno));
			exit(-1);
		}
		else
		{
			pc->data = tmp;
			pc->capacity += INC_SIZE;
			printf("扩容成功,当前容量为%d\n", pc->capacity);
		}
	}
}
void AddContact(Contact* pc)
{
	CheckCapacity(pc);

	printf("请输入姓名:>");
	scanf("%s", pc->data[pc->size].name);
	printf("请输入年龄:>");
	scanf("%d", &(pc->data[pc->size].age));
	printf("请输入性别:>");
	scanf("%s", pc->data[pc->size].sex);
	printf("请输入电话:>");
	scanf("%s", pc->data[pc->size].tele);
	printf("请输入地址:>");
	scanf("%s", pc->data[pc->size].addr);
	pc->size++;
}


void DestroyContact(Contact* pc)
{
	pc->capacity = 0;
	pc->size = 0;
	free(pc->data);
	pc->data = NULL;
	printf("销毁成功...\n");
}


void ShowContact(const Contact* pc)
{
	printf("%-10s %-10s %-10s %-20s %-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	for (int i = 0; i < pc->size; i++)
	{
		printf("%-10s %-10d %-10s %-20s %-30s\n", 
			pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
	}
}


static int FindByName(const Contact* pc, char name[])
{
	//找到删除人的下标
	for (int i = 0; i < pc->size; i++)
	{
		if (0 == strcmp(pc->data[i].name, name))
		{
			return i;
		}
	}
	return -1;
}


void DelContact(Contact* pc)
{
	if (pc->size == 0)
	{
		printf("通讯录为空，不可以删除\n");
		return;
	}
	char name[MAX_NAME];
	printf("请输入你想删除的人的姓名:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	
	if (-1 == pos)
	{
		printf("这个人不存在\n");
		return;
	}
	//删除
	for (int i = pos; i < pc->size - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->size--;
	printf("删除成功\n");
}


void SearchContact(const Contact* pc)
{
	char name[MAX_NAME];
	printf("请输入你想查找的人的姓名:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (-1 == pos)
	{
		printf("这个人不存在\n");
		return;
	}
	printf("%-10s %-10s %-10s %-20s %-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	printf("%-10s %-10d %-10s %-20s %-30s\n",
		pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr);
}



void ModifyContact(Contact* pc)
{
	char name[MAX_NAME];
	printf("请输入你要修改的人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (-1 == pos)
	{
		printf("这个人不存在\n");
		return;
	}
	printf("请输入姓名:>");
	scanf("%s", pc->data[pos].name);
	printf("请输入年龄:>");
	scanf("%d", &(pc->data[pos].age));
	printf("请输入性别:>");
	scanf("%s", pc->data[pos].sex);
	printf("请输入电话:>");
	scanf("%s", pc->data[pos].tele);
	printf("请输入地址:>");
	scanf("%s", pc->data[pos].addr);
	printf("修改成功\n");
}

int cmp_by_name(const void* n1, const void* n2)
{

	return strcmp(((PeoInfo*)n1)->name, ((PeoInfo*)n2)->name);
}

//按姓名排序
void SortContact(Contact* pc)
{
	qsort(pc->data, pc->size, sizeof(PeoInfo), cmp_by_name);
	printf("排序成功\n");
}


//导入文件
void SaveContact(Contact* pc)
{
	//打开文件
	FILE* pf = fopen("Contact.txt", "wb");
	if (NULL == pf)
	{
		perror("fopen");
		return;
	}

	//使用
	for (int i = 0; i < pc->size; i++)
	{
		fwrite(&(pc->data[i]), sizeof(PeoInfo), 1, pf);
	}
	//关闭文件
	fclose(pf);
	pf = NULL;
	printf("写入文件成功！\n");
}
